/*
 * Copyright (C) 2006-2015
 * 版权所有者为北京捷越联合信息咨询有限公司。本系统是商用软件,未经授权擅自复制或传播本程序的部分或全部将是非法的。
 * @title: XmlConvertUtil.java
 * @package com.jy.modules.cims.core.service.interact.util
 * @author zhaox
 * @date 2015-2-10 上午10:20:19
 * @version v1.00
 * @description: TODO(用一句话描述该文件做什么)
 */

package com.jy.modules.cims.interact.util;

import com.jy.modules.cims.data.interact.xintian.Root;
import com.jy.modules.cims.data.interact.xintian.error.EsbHead;
import com.jy.modules.cims.data.interact.xintian.error.Result;
import com.thoughtworks.xstream.XStream;

/**
 * 使用xstream转换dto，xml
 * @classname: XmlConvertUtil
 * @description: TODO(这里用一句话描述这个类的作用)
 */

public class XmlConvertUtil {

    /**
     * dto转xml
     * 
     * @title: convertDTO2Str
     * @author
     * @description:
     * @date 2015-2-9 上午9:56:36
     * @param root
     * @return
     * @throws
     */
    public static String convertDTO2Str(Root root) {
        XStream xStream = new XStream();
        xStream.setMode(XStream.NO_REFERENCES);
        xStream.autodetectAnnotations(true);
        String str = xStream.toXML(root);
        return str;
    }

    /**
     * xml转dto
     * 
     * @title: convertStr2DTO
     * @author
     * @description:
     * @date 2015-2-9 上午9:56:40
     * @param xml
     * @return
     * @throws
     */
    public static Root convertStr2DTO(String xml, Object root) {
        XStream xStream = new XStream();
        xStream.autodetectAnnotations(true);
        xStream.alias("Root", root.getClass());
        Root ret = (Root) xStream.fromXML(xml, root);
        return ret;
    }

    /**
     * xml转error
     * 
     * @title: convertStr2DTO
     * @author
     * @description:
     * @date 2015-2-9 上午9:56:40
     * @param xml
     * @return
     * @throws
     */
    public static Result convertStr2Error(String xml) {
        XStream xStream = new XStream();
        xStream.autodetectAnnotations(true);
        Result temple = new Result();
        EsbHead esbHead = new EsbHead();
        temple.setEsbhead(esbHead);
        xStream.alias("service", Result.class);
        Result result = (Result) xStream.fromXML(xml, temple);
        return result;
    }
}
